package com.ssh.model;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import java.io.Serializable;
import java.util.Date;

/**
 * Created by liaoh on 2017/5/5.
 */
@MappedSuperclass
public class BaseEntity implements Serializable {
    private String id;
    private Date createDate;
    private Date modifyDate;

    @Id
    @Column(length = 32, nullable = true)
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Column(updatable = false)
    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public Date getModifyDate() {
        return modifyDate;
    }

    public void setModifyDate(Date modifyDate) {
        this.modifyDate = modifyDate;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj){
            return true;
        }
        if (obj == null){
            return false;
        }
        if (getClass().getPackage() != obj.getClass().getPackage()){
            return false;
        }
        final BaseEntity other = (BaseEntity) obj;
        if (id == null){
            if (other.getId() !=null){
                return false;
            }
        }else if (!id.equals(other.getId())){
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        return  id==null?System.identityHashCode(this):id.hashCode();
    }
}
